package com.example.eshop_ssm.controller;

import cn.hutool.http.server.HttpServerRequest;
import com.example.eshop_ssm.annotation.CheckLogin;
import com.example.eshop_ssm.entity.AdImgInfo;
import com.example.eshop_ssm.entity.NewsInfo;
import com.example.eshop_ssm.service.AdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ad")
public class AdimgController {

    @Autowired
    private AdService adService;

    /***
     * lun bo ye mian
     * @return
     */
    @CheckLogin
    @RequestMapping("/rotation")
    public ModelAndView rotation() throws Exception{
        ModelAndView modelAndView = new ModelAndView();
        List<AdImgInfo> listImg=adService.selectImg();
        //存储图片转盘发在页面使用
        modelAndView.addObject("listImg",listImg);
        modelAndView.setViewName("adimg");
        return modelAndView;
    }

    /**
     * 添加轮播数据
     * @param ad_img
     * @param book_id
     * @return
     * @throws Exception
     */
    @RequestMapping("/addImgs")
    @ResponseBody
    public Map<String, Object> addImgs(
            @RequestParam("ad_img") String ad_img,
            @RequestParam("book_id") Integer book_id,
            HttpServletRequest request
    ) throws Exception {

        //  存储参数在Session中
        request.getSession().setAttribute("book_id",book_id);
        //添加进数据库
        boolean isok = adService.inseImg(ad_img,book_id);
        Map<String, Object> map = new HashMap<>();
        map.put("isok", isok);
        return map;
    }

    /**
     * 删除图片
     * @param ad_id
     * @return
     */
    @RequestMapping("/deleteImgs")
    @ResponseBody
    public Map<String,Object>deleteImgs(
           @RequestParam(value = "ad_id") Integer ad_id
    )throws Exception{
        boolean isok=adService.deleteImg(ad_id);

        Map<String, Object> map = new HashMap<>();
        map.put("isok", isok);
        return map;
    }

    /**
     * 查询公告板信息
     *
     * @param page
     * @param tiao
     * @return
     */
    @CheckLogin
    @RequestMapping("/adnews")
    public ModelAndView adNews(
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "tiao", defaultValue = "10") Integer tiao
    ) throws Exception {
        ModelAndView modelAndView = new ModelAndView();

        //查询公告板信息
        List<NewsInfo> newsInfoList = adService.selectNews(page, tiao);
        modelAndView.addObject("newsInfoList", newsInfoList);

        //查询公告板有多少条信息
        int count = adService.countNews();
        modelAndView.addObject("count", count);

        //计算有多少页
        int zoyeshu = count % tiao == 0 ? count / tiao : count / tiao + 1;
        modelAndView.addObject("zoyeshu", zoyeshu);

        //设置跳转的视图
        modelAndView.setViewName("adnews");

        return modelAndView;
    }


    /**
     * 添加公告板信息
     *
     * @param news_title
     * @param book_id
     * @return
     * @throws Exception
     */
    @RequestMapping("/insertnew")
    @ResponseBody
    public Map<String, Object> insertNew(
            @RequestParam("news_contents") String news_title,
            @RequestParam("book_id") Integer book_id
    ) throws Exception {
        Map<String, Object> map = new HashMap<>();
        //添加进数据库
        boolean isOk = adService.addNews(news_title, book_id);

        map.put("msg", isOk);
        return map;
    }

    /**
     * 删除公告板信息
     *
     * @param news_id
     * @return
     * @throws Exception
     */
    @RequestMapping("/delnew")
    @ResponseBody
    public Map<String, Object> delnew(
            @RequestParam("news_id") Integer news_id
    ) throws Exception {
        Map<String, Object> map = new HashMap<>();
        //删除公告板信息
        boolean isOk = adService.delNews(news_id);

        map.put("msg", isOk);
        return map;
    }

    /**
     * 跳转到修改公告板页面
     *
     * @param news_id
     * @return
     * @throws Exception
     */
    @RequestMapping("/toupdatenew/{news_id}")
    public ModelAndView toUpdateNew(
            @PathVariable("news_id") Integer news_id
    ) throws Exception {
        ModelAndView modelAndView = new ModelAndView();

        //查询公告板信息
        NewsInfo newsInfo = adService.selectOneNew(news_id);
        modelAndView.addObject("newsInfo", newsInfo);

        //设置跳转的视图
        modelAndView.setViewName("updatenews");

        return modelAndView;
    }

    /**
     * 修改公告板信息
     *
     * @param news_title
     * @param book_id
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateNew")
    @ResponseBody
    public Map<String, Object> updateNew(
            @RequestParam("news_id") Integer news_id,
            @RequestParam("news_title") String news_title,
            @RequestParam("book_id") Integer book_id
    ) throws Exception {
        Map<String, Object> map = new HashMap<>();
        //修改数据库信息
        boolean isOk = adService.updateNews(news_id, news_title, book_id);

        map.put("msg", isOk);
        return map;
    }
}
